clear;
% clc;
% warning off;
restoredefaultpath
addpath(genpath('evaluation'));
addpath(genpath('funs'));
addpath(genpath('measure'));

%% dataset
Dataset_Name = {
    'WebKB_Wisconsin2views',% 1
    'BDGP',% 2
    'CiteSeer',% 3
    'VGGFace2_100_4Views',% 4
    'CIFAR100_All_4Views',% 5
    'VGGFace2_200_4Views',% 6
    'TinyImageNet_4Views' % 7
    };

Dataset_Path = '.\datasets\MultiView Dataset\';
resPath = './Res/';

metric = {'ACC','NMI','Purity','Fscore','Precision','Recall','ARI','Entropy'};

%% Experiment
ACC_all=zeros(7,5);
for Data_index = [1 2] % 只运行前两个数据集
    % load data & make folder
    dataName = Dataset_Name{Data_index}; disp([num2str(Data_index) 9 dataName]);
    load(strcat(Dataset_Path,dataName));
    
    matpath = strcat(resPath,dataName); %保存结果
    txtpath = strcat(resPath,strcat(dataName,'.txt'));
    if (~exist(matpath,'file'))
        mkdir(matpath);
    end
    dlmwrite(txtpath, strcat('Dataset:',cellstr(dataName), '  Date:',datestr(now)),'-append','delimiter','','newline','pc');
    %%
    numsample = size(Y,1);
    numview = length(X); %原始输入的是 n * dp
    numclass = length(unique(Y));
    
    for p = 1:numview
        X{p} = mapstd(X{p}',0,1);    % dp * n 均值为0 方差为1
    end
    
    for p = 1:numview                %预处理 X*X'=0的情况
        index = sum(abs(X{p}),2) > 1e-8;
        X{p} = X{p}(index,:);
        X_dim(p) = sum(index);
    end
    X_dim_min = min(X_dim);
    
    Anchor_set = [1]*numclass; %
    gamma_set = [0]; %
    eta_set = 10.^[3 5 7 9 11];
    %%
    for Anchor_index = 1:length(Anchor_set)
        numanchor = Anchor_set(Anchor_index);
        if numanchor > numsample % if numanchor > X_dim_min || numanchor > numsample
            continue
        end
        for gamma_index = 1:length(gamma_set)
            gamma = gamma_set(gamma_index);
            for eta_index = 1:length(eta_set)
                eta = eta_set(eta_index);
                
                tic;
                [U,Z,Ap,alpha,obj] = LASD(X,numclass,numanchor,gamma,eta);
                t = toc;
                
                [res_max,res_mean,res_std,~,PreY]= myNMIACCwithmean_LL(U,Y,numclass);
                ACC_all(Data_index,eta_index) = res_mean(1);
                
                fprintf('Anchor:%4.0f \t gamma:%4.4f \t eta:%4.4f \t ACC:%4.2f \t NMI:%4.2f \t Pur:%4.2f \t Fscore:%4.2f \n',...
                    [numanchor gamma eta res_mean(1)*100 res_mean(2)*100 res_mean(3)*100 res_mean(4)*100]);
                matname = [Dataset_Name{Data_index},...
                    '_Anch=',num2str(numanchor/numclass),'k(',num2str(numanchor),')',...
                    '_gamma_(',num2str(gamma),')'...'_gamma_10^(',num2str(log10(gamma)),')'
                    '_eta_10^(',num2str(log10(eta)),')'...
                    '_ACCmean_',num2str(res_mean(1)*100,'%.2f'),'_NMImean_',num2str(res_mean(2)*100,'%.2f'),...
                    '_Purmean_',num2str(res_mean(3)*100,'%.2f'),'_Fscoremean_',num2str(res_mean(4)*100,'%.2f')];
                save([matpath,'/',matname,'.mat'],...
                    'U','Z','Ap','alpha','obj','eta','gamma','PreY','t',...
                    'res_max','res_mean','res_std');
            end
        end
    end
    ACC_all(Data_index,:)
%     clear Anchor_set feature_set
end

